import { templateRef } from '@vueuse/core'
import { onMounted } from 'vue'
/**
 * 劫持remote-select父级的change事件，触发change事件
 * 清空当前remote-select的值
 **/

// 配置RemoteSelect，和关联关系
export default function useRemoteSelect(ref: string, ...parentRefs: string[]) {
  if (parentRefs.length == 0) return
  onMounted(() => {
    const selfRMInstance = templateRef(ref)
    parentRefs.forEach((it) => {
      const parentRMInstance = templateRef(it)
      if (parentRMInstance && parentRMInstance.value) {
        parentRMInstance.value.hookChange.change = () => {
          if (selfRMInstance && selfRMInstance.value) {
            selfRMInstance.value.clearValue()
          }
        }
      }
    })
  })
}
